package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.DisMemberInfo;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.PartnerListService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/partnerList")
public class PartnerListController extends BaseController {
    @Resource
    private PartnerListService partnerListService;

    private String PREFIX = "/packback/partnerList/";

    @RequestMapping("")
    public String index(){
        return PREFIX+"partnerList.html";
    }


    /**
     * 跳转到修改申请页面
     */
    @RequestMapping(value = "/partner_update/{disMemberInfoId}")
    public Object joinRebateUpdate(@PathVariable Integer disMemberInfoId, Model model){
        DisMemberInfo disMemberInfo= partnerListService.selectById(disMemberInfoId);
        Map<String, Object> disMemberInfomap = BeanKit.beanToMap(disMemberInfo);
        System.out.println(disMemberInfomap);
        model.addAttribute("partnerList",disMemberInfomap);
        LogObjectHolder.me().set(disMemberInfo);
        return PREFIX+"partnerList_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String order,Integer offset,Integer limit,String disUsername,String bindingPhone,String recomm_pcode,Integer status,String jointime,String jointime_end) {
        Map<String, Object> map = new HashMap<>();
        map.put("order",order);
        map.put("offset",offset);
        map.put("limit",limit);
        map.put("dis_user_name",disUsername);
        map.put("bindingPhone",bindingPhone);
        map.put("recomm_pcode",recomm_pcode);
        map.put("status",status);
        map.put("jointime",jointime);
        map.put("jointime_end",jointime_end);

        List<DisMemberInfo> list= partnerListService.selectList(map);
        for(int i = 0; i < list.size();i++ ){
            if(list.get(i).getStatus() == 0){
                list.get(i).setZhuangtai("启用");
            }else{
                list.get(i).setZhuangtai("停用");
            }
        }
        System.out.println(list);
        return list;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid DisMemberInfo disMemberInfo){
        DisMemberInfo disMemberInfo_temp= partnerListService.selectById(disMemberInfo.getId());
        if(disMemberInfo.getStatus() != disMemberInfo_temp.getStatus()){
            if(disMemberInfo.getStatus() == 1){
                Date date = new Date();
                disMemberInfo.setStupUse_time(date);
                partnerListService.update(disMemberInfo);
            }else{
                partnerListService.use(disMemberInfo);
            }
        }
        return super.SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        partnerListService.delete(id);
        return super.SUCCESS_TIP;
    }

    /**
     * 停用
     */
    @RequestMapping(value = "/stopUse",method = RequestMethod.POST)
    @ResponseBody
    public Object stopUse(Integer id){
        partnerListService.stopUse(id);
        return super.SUCCESS_TIP;
    }



    /**
     * 合同详情
     */
    @RequestMapping(value = "/contractDetail/{PartnerId}")
    @ResponseBody
    public Object contractDetail(@PathVariable Integer PartnerId, Model model){
        return PREFIX+"disMemberInfo_detail.html";
    }
    /**
     * 导出
     */
    @RequestMapping(value = "/excelList",method = RequestMethod.POST)
    @ResponseBody
    public Object excelList(HttpServletResponse response,String disUsername, String bindingPhone, String recomm_pcode, Integer status, String jointime, String jointime_end){
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("dis_user_name",disUsername);
        map.put("bindingPhone",bindingPhone);
        map.put("recomm_pcode",recomm_pcode);
        map.put("status",status);
        map.put("jointime",jointime);
        map.put("jointime_end",jointime_end);

        Map<String, Object> result=new HashMap<String, Object>();
        try {
            partnerListService.excelList(map,response);
            result.put("code",200);
            result.put("msg","操作成功");
        }catch (Exception e){
            result.put("code",201);
            result.put("msg","操作成功");
        }
        return result;
    }
}
